$(function() {
	$("#jqGrid")
			.jqGrid(
					{
						url : '../exchanges/list',
						datatype : "json",
						colModel : [
								{
									label : '交换机编号',
									name : 'exchangesId',
									width : 50,
									key : true,
									hidden : true
								},
								{
									label : '交换机名称',
									name : 'exchangesName',
									width : 80
								},
								{
									label : '使用状态',
									name : 'flag',
									width : 50,
									formatter : function(value, options, row) {
										return value === 0 ? '<span class="label label-danger">未使用</span>'
												: '<span class="label label-success">已使用</span>';
									}
								}, {
									label : '交换机类型',
									name : 'exchangesType',
									width : 50
								}, {
									label : '持久化',
									name : 'exDurable',
									width : 80
								}, {
									label : '自动删除',
									name : 'exAutodelete',
									width : 50
								}, {
									label : '交换机描述',
									name : 'exComments',
									width : 80
								} ],
						viewrecords : true,
						height : 385,
						rowNum : 10,
						rowList : [ 10, 30, 50 ],
						rownumbers : true,
						rownumWidth : 25,
						autowidth : true,
						multiselect : true,
						pager : "#jqGridPager",
						jsonReader : {
							root : "page.list",
							page : "page.currPage",
							total : "page.totalPage",
							records : "page.totalCount"
						},
						prmNames : {
							page : "page",
							rows : "limit",
							order : "order"
						},
						gridComplete : function() {
							// 隐藏grid底部滚动条
							$("#jqGrid").closest(".ui-jqgrid-bdiv").css({
								"overflow-x" : "hidden"
							});
						}
					});
	/*$('input[id="apliCode"]').click(function() {
		alert("您是..." + $(this).val());
		$('#exchangesName').val($(this).val());
	});*/
});

//页面验证
var exchanges;
$(function() {
	exchanges = $("#exchanges").Validform({
		tiptype : 3,
		//showAllError:true,
		datatype : {
			//匹配是否合法(字母开头，允许5-16字节，允许字母数字下划线)
			"exchangesName" : /^[a-zA-Z][a-zA-Z0-9_]{2,15}$/,
		},

	});
	exchanges.addRule([ {
		ele : ".form-control:eq(0)",
		datatype : "exchangesName",
		nullmsg : "请输入交换机名称",
		errormsg : "请输入正确格式(字母数字下划线2－15位)"
		
	},
	{
		ele : ".form-control:eq(1)",
		datatype : "s2-16",
		nullmsg : "请输入交换机描述",
		errormsg : "交换机描述至少2个字符,最多16个字符！"
		
	}

	]);
});

var vm = new Vue({
	el : '#rrapp',
	data : {
		showList : true,
		title : null,
		taplicationList : {},
		exchanges : {}

	},
	methods : {
		query : function() {
			vm.reload();
		},
		add : function() {
			vm.showList = false;
			vm.title = "新增";
			vm.taplicationList = {};
			vm.exchanges = {
				exAutodelete :false,
				exDurable : true,
				exchangesType : "direct",
				flag : "0"
			};
			// 获取应用信息
			this.getTaplicationListList();
		},
		update : function(event) {
			var exchangesId = getSelectedRow();
			if (exchangesId == null) {
				return;
			}
			vm.showList = false;
			vm.title = "修改";

			vm.getInfo(exchangesId);
			// 获取应用信息
			this.getTaplicationListList();
		},
		saveOrUpdate : function(event) {
			//表单提交前验证
			var bool = exchanges.check();
			if (!bool) {
				return false;
			}
			var url = vm.exchanges.exchangesId == null ? "../exchanges/save"
					: "../exchanges/update";
			$.ajax({
				type : "POST",
				url : url,
				data : JSON.stringify(vm.exchanges),
				success : function(r) {
					if (r.code === 0) {
						alert('操作成功', function(index) {
							vm.reload();
						});
					} else {
						alert(r.msg);
					}
				}
			});
		},
		del : function(event) {
			var exchangesIds = getSelectedRows();
			if (exchangesIds == null) {
				return;
			}

			confirm('确定要删除选中的记录？', function() {
				$.ajax({
					type : "POST",
					url : "../exchanges/delete",
					data : JSON.stringify(exchangesIds),
					success : function(r) {
						if (r.code == 0) {
							alert('操作成功', function(index) {
								$("#jqGrid").trigger("reloadGrid");
							});
						} else {
							alert(r.msg);
						}
					}
				});
			});
		},
		getInfo : function(exchangesId) {
			$.get("../exchanges/info/" + exchangesId, function(r) {
				vm.exchanges = r.exchanges;
			});
		},
		getTaplicationListList : function() {

			$.get("../taplication/select", function(r) {
				vm.taplicationList = r.list;
			});
		},
		reload : function(event) {
			vm.showList = true;
			var page = $("#jqGrid").jqGrid('getGridParam', 'page');
			$("#jqGrid").jqGrid('setGridParam', {
				page : page
			}).trigger("reloadGrid");
		}
	}
});